/*
 * Camera.cpp
 *
 *  Created on: 14 févr. 2011
 *      Author: elendir
 */

#include "Camera.h"
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glext.h>
#include <GL/glx.h>
#include <math.h>

Camera::Camera(Vector3 position, Vector3 lookAt, Vector3 up) {
	this->position = position;
	this->lookAt = lookAt;
	this->up = up;
	this->m_strafeZ = position.GetZ();
	m_strafeX = 0.0;
	rotX = 0.0;
	rotY = 0.0;
}

Camera::~Camera() {
}

void Camera::strafeX(bool left) {
	//m_strafeX += left?0.1:-0.1;
	//glMatrixMode(GL_PROJECTION);
	//float x = left?0.1:-0.1;
	//position = position + Vector3((double)x, 0.0, 0.0);
	//glTranslatef(x, 0.0, 0.0);
	Vector3  direction = (lookAt-position).Normalize(1);
	Vector3 right = (direction^up).Normalize(0.1);
	if(left) {
		position = position - right;
		lookAt = lookAt - right;
		//glTranslatef(right.GetX(), right.GetY(), right.GetZ());
	}
	else {
		position = position + right;
		lookAt = lookAt + right;
		//glTranslatef(-right.GetX(), -right.GetY(), -right.GetZ());
	}
	Update();
}

void Camera::strafeZ(bool forward) {
	//m_strafeZ += forward?0.1:-0.1;
	//glMatrixMode(GL_MODELVIEW);
	Vector3  direction = (lookAt-position).Normalize(0.1);
	if(forward) {
		position = position+direction;
		//glTranslatef(-direction.GetX(), -direction.GetY(), -direction.GetZ());
	}
	else {
		position = position-direction;
		//glTranslatef(direction.GetX(), direction.GetY(), direction.GetZ());
	}
	//glMatrixMode(GL_PROJECTION);
	//float z = forward?0.1:-0.1;
	//position = position + Vector3(0.0, 0.0, (double)z);
	//glTranslatef(0.0, 0.0, z);
	Update();
}



void Camera::RotateUp(bool up) {
	glMatrixMode(GL_MODELVIEW);
	//glTranslatef(position.GetX(), position.GetY(), position.GetZ());
	float rot = up?-0.5:0.5;
	glRotatef(rot, 1.0, 0.0, 0.0);
	//glTranslatef(-position.GetX(), -position.GetY(), -position.GetZ());

}

void Camera::RotateLeft(bool left) {
	float rot = left?-0.5:0.5;;
	glMatrixMode(GL_MODELVIEW);
	glRotatef(rot, 0.0, 1.0, 0.0);

	//glTranslatef(position.GetX(), position.GetY(), position.GetZ());
	//float rot = left?-0.5:0.5;
	//glRotatef(rot, 0.0, 1.0, 0.0);
	//glTranslatef(-position.GetX(), -position.GetY(), -position.GetZ());
}

void Camera::Update() {
		//glMatrixMode(GL_MODELVIEW);
		//
		//glTranslatef(position.GetX(), position.GetY(), position.GetZ());

		//glTranslatef(0.0, 0.0, m_strafeZ);

		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		gluLookAt(position.GetX(), position.GetY(), position.GetZ(), lookAt.GetX(), lookAt.GetY(), lookAt.GetZ(), up.GetX(), up.GetY(), up.GetZ());

}
